package com.yuexunit.mobilenurse.module.BloodSugarQuery.ui;

import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.base.titlebar.TitleBar_DocAdvice;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodSugarQuery.adapter.BloodSugarAdapter;
import com.yuexunit.mobilenurse.module.BloodSugarQuery.bean.BloodSugarBean;
import com.yuexunit.mobilenurse.module.BloodSugarQuery.model.impl.BloodSugarModel;
import com.yuexunit.mobilenurse.module.BloodSugarQuery.presenter.IBloodSugarPresenter;
import com.yuexunit.mobilenurse.module.BloodSugarQuery.presenter.impl.BloodSugarPresenter;
import com.yuexunit.mobilenurse.module.BloodSugarQuery.ui.view.IBloodSugarQueryView;
import com.yuexunit.mobilenurse.module.SignQuery.bean.YXSignQuery;
import com.yuexunit.mobilenurse.util.ProUtil;
import com.yuexunit.mobilenurse.widget.FixedSwipeListView;
import com.yuexunit.mobilenurse.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/12 0012.
 */

public class Act_BloodSugarQuery extends TitleBar_DocAdvice implements IBloodSugarQueryView{
    @Bind(R.id.titlebar_docadvice_tv_title)
    TextView titlebarDocadviceTvTitle;
    @Bind(R.id.docactvice_name)
    TextView docactviceName;
    @Bind(R.id.docactvice_bedno)
    TextView docactviceBedno;
    @Bind(R.id.docactvice_visitno)
    TextView docactviceVisitno;
    @Bind(R.id.cost_data)
    TextView costData;
    @Bind(R.id.bloodsugar_bf)
    TextView bloodsugarBf;
    @Bind(R.id.bloodsugar_af)
    TextView bloodsugarAf;
    @Bind(R.id.bloodsugar_bl)
    TextView bloodsugarBl;
    @Bind(R.id.bloodsugar_al)
    TextView bloodsugarAl;
    @Bind(R.id.bloodsugar_bd)
    TextView bloodsugarBd;
    @Bind(R.id.bloodsugar_ad)
    TextView bloodsugarAd;
    @Bind(R.id.blood_sugar_list)
    FixedSwipeListView bloodSugarList;

    IBloodSugarPresenter presenter;
    //病人住院号
    private String Visitno;
    private String admissiondate;
    SimpleDateFormat formatter;
    Calendar calendar = Calendar.getInstance();
    //距离入院天数
    private int day = 0;
    private int countday = 0;

    LoadingDialog dialog;
    public static final int SHOW_DIALOG = 1;
    public static final int DISMISS_DIALOG = -1;
    public static final int INCREASE_COST_DAY = 1;
    public static final int DECREASE_COST_DAY = -1;

    BaseAdapter adapter;

    ArrayList<BloodSugarBean> bloodSugarBeans = new ArrayList<BloodSugarBean>();
    ArrayList<YXSignQuery.SignQueryEntity> signQueryEntityArrayList = new ArrayList<YXSignQuery.SignQueryEntity>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_bloodsugarquery);
        ButterKnife.bind(this);
    }

    @Override
    public void setTitle()
    {
        titlebarDocadviceTvTitle.setText("血糖监测记录");
        docactviceName.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_NAME));
        docactviceBedno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_BEDCODE));
        docactviceVisitno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO));
    }

    private void set_time() {
        String str = formatter.format(calendar.getTime());
        costData.setText(str);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new BloodSugarPresenter(new BloodSugarModel(),this);
        Visitno = PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO);
        admissiondate = PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_DATE);
        formatter = new SimpleDateFormat("yyyy-MM-dd",java.util.Locale.getDefault());
        calendar.setTime(new Date());//获取当前时间
        String str = formatter.format(calendar.getTime());
        try {
            day = daysBetween(admissiondate,str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        set_time();
        if (SystemTool.checkNet(aty)) {
            this.dialogState(SHOW_DIALOG);
            presenter.showBloodSugarInfoFive(Visitno, "1",str);
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
//            log.error(CreateJson.LOG_JSON("", "1", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_DEPTNO), ""));
        }
    }

    @Override
    public  void onBackClick()
    {
        finish();
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.cost_left, R.id.cost_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.cost_left:
                if(day > 0) {
                    if (!ProUtil.isFastClick()) {
                        choseCostDay(DECREASE_COST_DAY);
                    }
                }
                break;
            case R.id.cost_right:
                if (countday > 0) {
                    if (!ProUtil.isFastClick()) {
                        choseCostDay(INCREASE_COST_DAY);
                    }
                }
                break;
        }
    }

    @Override
    public void showBloodSugarInfo(ArrayList<YXSignQuery.SignQueryEntity> signQueryEntities) {
        signQueryEntityArrayList.clear();
        for(int i=0;i<signQueryEntities.size();i++)
        {
            switch (signQueryEntities.get(i).getVital_signs())
            {
                case "早餐前":
                    bloodsugarBf.setText(signQueryEntities.get(i).getVital_signs_values_c());
                    break;
                case "早餐后":
                    bloodsugarAf.setText(signQueryEntities.get(i).getVital_signs_values_c());
                    break;
                case "午餐前":
                    bloodsugarBl.setText(signQueryEntities.get(i).getVital_signs_values_c());
                    break;
                case "午餐后":
                    bloodsugarAl.setText(signQueryEntities.get(i).getVital_signs_values_c());
                    break;
                case "晚餐前":
                    bloodsugarBd.setText(signQueryEntities.get(i).getVital_signs_values_c());
                    break;
                case "晚餐后":
                    bloodsugarAd.setText(signQueryEntities.get(i).getVital_signs_values_c());
                    break;
                default:
                    signQueryEntityArrayList.add(signQueryEntities.get(i));
                        break;
            }
        }

        for(int i = 0;i<signQueryEntityArrayList.size();i++)
        {
            if(signQueryEntityArrayList.get(i).getVital_signs().contains("临时时间"))
            {
                BloodSugarBean bloodSugarBean = new BloodSugarBean();
                bloodSugarBean.setTime(signQueryEntityArrayList.get(i).getVital_signs_values_c());
               for (int j =0;j<signQueryEntityArrayList.size();j++)
               {
                   if(signQueryEntityArrayList.get(i).getVital_signs().substring(signQueryEntityArrayList.get(i).getVital_signs().length()-1, signQueryEntityArrayList.get(i).getVital_signs().length()).equals(
                           signQueryEntityArrayList.get(j).getVital_signs().substring(signQueryEntityArrayList.get(j).getVital_signs().length()-1, signQueryEntityArrayList.get(j).getVital_signs().length())
                   ))
                   {
                       bloodSugarBean.setData(signQueryEntityArrayList.get(j).getVital_signs_values_c());
                   }
               }
                bloodSugarBeans.add(bloodSugarBean);
            }
        }

        adapter = new BloodSugarAdapter(bloodSugarList,bloodSugarBeans,R.layout.item_blood_sugar);
        bloodSugarList.setAdapter(adapter);
    }

    @Override
    public void dialogState(int status) {
        switch (status){
            case SHOW_DIALOG:
                dialog = new LoadingDialog(aty);
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                break;
            case DISMISS_DIALOG:
                dialog.dismiss();
                break;
        }
    }

    public void choseCostDay(int choose){
        if (SystemTool.checkNet(aty)) {
            this.dialogState(SHOW_DIALOG);
            calendar.add(Calendar.DATE, choose);
            set_time();
            countday = countday-choose;
            day = day+choose;
            bloodSugarBeans.clear();
            adapter.notifyDataSetChanged();
            bloodsugarBf.setText("");
            bloodsugarAf.setText("");
            bloodsugarBl.setText("");
            bloodsugarAl.setText("");
            bloodsugarBd.setText("");
            bloodsugarAd.setText("");
            presenter.showBloodSugarInfoFive(Visitno,"1" ,formatter.format(calendar.getTime()));
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
//            log.error(CreateJson.LOG_JSON("", "1", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_DEPTNO), ""));
        }
    }

    /**
     *字符串的日期格式的计算
     */
    public static int daysBetween(String smdate,String bdate) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));
    }
}
